﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using WEBRM.Dominio.Servicos;
using WEBRM.Repositorio.Repositorios;
using WEBRM.Dominio.Entidades;

namespace WEBRM.WebUI
{
    public partial class frmStatusAtendimento : System.Web.UI.Page
    {
        private StatusServico statusServico;

        protected void Page_Load(object sender, EventArgs e)
        {
            AdicionarTitulosPagina();
            if (!IsPostBack)
                PopulateGrid();
        }

        private void AdicionarTitulosPagina()
        {
            ((Site)Master).Titulo = "Status do atendimento";
            ((Site)Master).Descricao = "Cadastre as informações do status do atendimento.";
        }

        protected void btnNovo_Click(object sender, EventArgs e)
        {
            NovoRegistro();
        }

        void NovoRegistro()
        {
            btnNovo.Visible = false;
            btnPesquisar.Visible = false;
            pnlGrid.Visible = false;
            pnlCadastro.Visible = true;
            pnlPesquisa.Visible = false;
        }

        void LimpaCampos()
        {
            txtPergunta.Text = "";
            chkPadrao.Checked = false;
            chkEncerramento.Checked = false;
        }
        void Cancelar()
        {
            btnNovo.Visible = true;
            btnPesquisar.Visible = true;
            pnlGrid.Visible = true;
            pnlCadastro.Visible = false;
            pnlPesquisa.Visible = true;
        }

        void  PopulateGrid()
        {
            statusServico = new StatusServico(new StatusRepositorio());
            gv.DataSource = statusServico.Repositorio.ObterTodos();
            gv.DataBind();
        }


        protected void btnPesquisar_Click(object sender, EventArgs e)
        {

        }

        protected void btnSalvar_Click(object sender, EventArgs e)
        {
            statusServico = new StatusServico(new StatusRepositorio());

            Status status = new Status();
            status.Nome = txtPergunta.Text;
            status.Padrao = chkPadrao.Checked;
            status.Conclusao = chkEncerramento.Checked;
            statusServico.Repositorio.Salvar(status);

            Cancelar();
            LimpaCampos();
            PopulateGrid();
        }

        protected void btnCancelar_Click(object sender, EventArgs e)
        {
            Cancelar();
            LimpaCampos();
        }

        protected void gv_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int rowId = int.Parse(e.CommandArgument.ToString());
            statusServico = new StatusServico(new StatusRepositorio());
            var status = statusServico.Repositorio.Obter(rowId);

            switch (e.CommandName.ToString().ToUpper())
            {
                case "EDITAR":
                    txtPergunta.Text = status.Nome;
                    chkPadrao.Checked = status.Padrao;
                    chkEncerramento.Checked = status.Conclusao;
                    NovoRegistro();
                    break;
                case "EXCLUIR":
                    statusServico.Repositorio.Excluir(status);
                    PopulateGrid();
                    break;
            }
        }
    }
}
